package com.imooc.stack;

import java.util.Stack;

/**
 * 用栈写一段匹配字符的代码
 */
public class Solution {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i ++) {
            char c = s.charAt(i);
            if(c == '(' || c == '{' || c == '[')
                stack.push(c);
            else {
                if(stack.isEmpty())
                    return false;
                if(c == stack.pop()) {
                    return true;
                }
            }
        }
        if(stack.isEmpty())
            return true;
        return false;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        System.out.println(solution.isValid("{[()]}"));
        System.out.println(solution.isValid("{[(]}"));
    }
}
